//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.live.standard.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.live.app.service.CacheService;
import com.ruoyi.live.base.domain.*;
import com.ruoyi.live.base.mapper.LiveExamObjectiveScoreDetailMapper;
import com.ruoyi.live.base.mapper.LiveProductSourceMapper;
import com.ruoyi.live.base.mapper.LiveTaskUserDetailMapper;
import com.ruoyi.live.standard.service.CountResult;
import com.ruoyi.live.standard.service.StandardService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class FourStandardImpl implements StandardService {
    private static final Logger log = LoggerFactory.getLogger(FourStandardImpl.class);
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    LiveProductSourceMapper sourceDao;
    @Autowired
    LiveTaskUserDetailMapper detailDao;
    @Autowired
    CacheService cacheService;
    @Autowired
    LiveExamObjectiveScoreDetailMapper scoreDetailDao;

    public FourStandardImpl() {
    }

    public String getStandardCode() {
        return "SCORE_RULE_PRODUCT_FOUR";
    }

    public CountResult doCountScore(LiveExamUser examUser, Long scoreId) {
        LiveScoreStandard standard = this.cacheService.getStandardByCode(this.getStandardCode());
        List<LiveTaskUser> tasks = examUser.getTaskUserList();
        Iterator var5 = tasks.iterator();

        while(var5.hasNext()) {
            LiveTaskUser task = (LiveTaskUser)var5.next();
            if (task.getTaskType() == 1) {
                LiveExamObjectiveScoreDetail scoreDetail = (new LiveExamObjectiveScoreDetail()).setTaskUserId(task.getId()).setScoreId(scoreId).setState(1).setProductId(task.getProductId());

                try {
                    if (standard == null) {
                        scoreDetail.setState(2).setErrDesc("获取评分标准失败,code为SCORE_RULE_PRODUCT_FOUR");
                        this.scoreDetailDao.insert(scoreDetail);
                        break;
                    }

                    scoreDetail.setStandardId(standard.getId()).setStandardName(standard.getStandardName());
                    Map<Integer, List<Long>> scoreItems = this.cacheService.getAllSourceByProductId(task.getProductId());
                    Double score = 0.0D;
                    List<LiveTaskUserDetail> taskUserDetails = this.detailDao.selectList(new QueryWrapper((new LiveTaskUserDetail()).setTaskUserId(task.getId()).setDetailType(2)));
                    Iterator var11 = taskUserDetails.iterator();

                    while(var11.hasNext()) {
                        LiveTaskUserDetail detail = (LiveTaskUserDetail)var11.next();
                        if (((List)scoreItems.get(detail.getDetailOrder())).contains(detail.getProductSourceId())) {
                            score = score + standard.getStandardValue().doubleValue();
                        }
                    }

                    scoreDetail.setHasScore(1).setScore(new BigDecimal(score));
                    this.scoreDetailDao.insert(scoreDetail);
                } catch (Exception var13) {
                    var13.printStackTrace();
                    log.error("LiveExamUser的id为{}计算得分出错，SCORE_RULE_PRODUCT_FOUR", examUser.getId(), var13);
                    scoreDetail.setState(2).setErrDesc("计算得分出错，SCORE_RULE_PRODUCT_FOUR");
                    this.scoreDetailDao.insert(scoreDetail);
                }
            }
        }

        return new CountResult(0.0D);
    }
}
